<?php

namespace app\api\controller;

use app\api\service\impl\StudentServiceImpl;
use app\api\service\StudentService;
use app\api\validate\Student as StudentValidate;
use app\Safe;
use think\App;
use think\facade\View;

/**
 * @package app\api\controller
 * @author Evan
 * @date 2024/5/12 21:15
 * @description Api控制器
 */
class Student extends Safe
{

    private StudentService $studentService;

    /**
     * 构造函数
     * @param StudentServiceImpl $studentServiceImpl
     * @param App $app
     */
    public function __construct(StudentServiceImpl $studentServiceImpl, App $app)
    {
        $this->studentService = $studentServiceImpl;
        parent::__construct($app);
    }

    /**
     * 返回View视图
     * @return string
     */
    public function index(): string
    {
        return View::fetch('api@student/index');
    }

    public function getAllStudent(): bool|string
    {
        return $this->studentService->getAllStudent();
    }

    public function getStudentById(): bool|string
    {
        //TODO 验证器
        $validate = new StudentValidate();
        if (!$validate->scene('getById')->check(input())) {
            return CustomJsonResult(400, $validate->getError());
        }
        return $this->studentService->getStudentById();
    }

    public function addStudent(): bool|string
    {
        //TODO 验证器
        $validate = new StudentValidate();
        if (!$validate->scene('add')->check(input())) {
            return CustomJsonResult(400, $validate->getError());
        }
        return $this->studentService->addStudent();
    }

    public function updateStudent(): bool|string
    {
        //TODO 验证器
        $validate = new StudentValidate();
        if (!$validate->scene('update')->check(input())) {
            return CustomJsonResult(400, $validate->getError());
        }
        return $this->studentService->updateStudent();
    }

    public function deleteStudent(): bool|string
    {
        //TODO 验证器
        $validate = new StudentValidate();
        if (!$validate->scene('delete')->check(input())) {
            return CustomJsonResult(400, $validate->getError());
        }
        return $this->studentService->deleteStudent();
    }
}